package cm.mefobe.android.telecomcost.dialog;

import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.activities.TelecomcostActivity;
import cm.mefobe.android.telecomcost.adapter.WeeklyThresholdAdapter;
import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;
import cm.mefobe.android.telecomcost.view.MonthlyThresholdSpinner;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemSelectedListener;

public class WeeklyThresholdPickerDialog extends Dialog {
		
	private Button btnSelect;
	private Button btnCloseWeeklyThresholdPickerDialog;
	private ListView week_list;
	private MonthlyThresholdSpinner monthly_threshold_list;	
	private TelecomcostActivity _context;

	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public WeeklyThresholdPickerDialog(Context context) {
		super(context, true, null);		
		_context = (TelecomcostActivity) context;
	}

	/**
	 * @param context
	 * @param theme
	 */
	public WeeklyThresholdPickerDialog(Context context, int theme) {
		super(context, theme);
		_context = (TelecomcostActivity) context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("WeeklyThresholdPickerDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.weekly_treshold_view);		
		setTitle("Select a weekly threshold");

		week_list = (ListView) findViewById(R.id.listViewThreshold);
		monthly_threshold_list = (MonthlyThresholdSpinner) findViewById(R.id.spinnerMonths);				

		monthly_threshold_list
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						bindWeeks();
					}

					public void onNothingSelected(AdapterView<?> arg0) {
					}
				});
		
		week_list.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				selectItem(arg0,arg2);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnSelect = (Button) findViewById(R.id.btnSave);
		btnCloseWeeklyThresholdPickerDialog = (Button) findViewById(R.id.btnClose);						

		btnSelect.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				selectTreshold();
			}						
		});

		btnCloseWeeklyThresholdPickerDialog.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {	
				closeDialog();
			}
		});
		
		Log.i("WeeklyThresholdPickerDialog","Dialog ohne bundle created....");
	}	
	
	protected void selectItem(AdapterView<?> arg0, int arg2) {
		if(_context != null)
		{
			WeeklyThreshold threshold = (WeeklyThreshold) arg0.getAdapter().getItem(arg2);
			Bundle data = new Bundle();
			data.putString(WeeklyThreshold.PROP_TYPE, TelecomCost.getGson().toJson(threshold));
			_context.setData(data);
		}		
	}

	private void bindWeeks() {			
		try {
			Log.i("WeeklyThresholdPickerDialog","binding the weekly thresholds of month " + monthly_threshold_list.getSelectedThreshold().getMonthNameLong());
			week_list.setAdapter(new WeeklyThresholdAdapter(
					_context, R.layout.threshold_view,
					monthly_threshold_list.getSelectedThreshold().getWeeklyThresholds()));
		} catch (Exception e) {			
			e.printStackTrace();
		}
	}
	
	protected void closeDialog() {
		Log.i("WeeklyThresholdPickerDialog","closing the dialog");
		hide();		
	}

	private void selectTreshold() {		
	}	

	@Override
	public void show() {
		Log.i("WeeklyThresholdPickerDialog", "showing the dialog...");		
		super.show();
		Log.i("WeeklyThresholdPickerDialog", "dialog on screen...binding data...");
		bindData();
	}
	
	private void bindData() {
		monthly_threshold_list.setAdapter(null);
	}

	@Override
	protected void onStart() {
		Log.i("WeeklyThresholdPickerDialog", "Starting the dialog...");
		super.onStart();
		Log.i("WeeklyThresholdPickerDialog", "dialog started...");
	}

	@Override
	protected void onStop() {
		Log.i("WeeklyThresholdPickerDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("WeeklyThresholdPickerDialog", "dialog stopped...");
	}

}
